package com.qxzn.module.mine.activity;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.support.v7.widget.LinearLayoutManager;
import android.text.TextUtils;
import android.view.View;

import com.alibaba.fastjson.JSONObject;
import com.github.jdsjlzx.interfaces.OnRefreshListener;
import com.github.jdsjlzx.recyclerview.LRecyclerView;
import com.github.jdsjlzx.recyclerview.LRecyclerViewAdapter;
import com.qxzn.app.bean.BaseBean;
import com.qxzn.app.bean.RobotDeviceListBean;
import com.qxzn.app.page.BaseActivity;
import com.qxzn.app.page.interfaces.MyInterface;
import com.qxzn.app.page.model.MineModel;
import com.qxzn.app.util.AppUtil;
import com.qxzn.app.util.SharePreferenceUtil;
import com.qxzn.module.mine.R;
import com.qxzn.module.mine.R2;
import com.qxzn.module.mine.adapter.RobotListAdapter;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;

/**
 * 切换机器人
 */
public class RobotListActivity extends BaseActivity implements MyInterface.GetRobotListListener, MyInterface.ChangeRobotListener {

    @BindView(R2.id.lrv_robot_list)
    LRecyclerView lrvRobotList;
    @BindView(R2.id.view_empty)
    View viewEmpty;
    private LRecyclerViewAdapter recyclerViewAdapter;
    private List<RobotDeviceListBean.DataBean> dataList = new ArrayList();
    private int page = 1;
    private int pageSize = 10;
    private RobotListAdapter adapter;
    private MineModel mineModel;

    @Override
    protected int initLayout() {
        return R.layout.activity_robot_list;
    }

    @Override
    protected void initView() {
        adapter = new RobotListAdapter(this, dataList);
        recyclerViewAdapter = new LRecyclerViewAdapter(adapter);
        lrvRobotList.setLayoutManager(new LinearLayoutManager(this));
        lrvRobotList.setEmptyView(viewEmpty);
        lrvRobotList.setAdapter(recyclerViewAdapter);
        lrvRobotList.setOnRefreshListener(new OnRefreshListener() {
            @Override
            public void onRefresh() {
                AppUtil.showLog("setOnRefreshListener");
                page = 1;
                getData();
            }
        });
//        lrvRobotList.setOnLoadMoreListener(new OnLoadMoreListener() {
//            @Override
//            public void onLoadMore() {
//                AppUtil.showLog("setOnLoadMoreListener");
//                page++;
//                getData();
//            }
//        });
        viewEmpty.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                lrvRobotList.refresh();
            }
        });
    }

    @Override
    protected void iniTitleBar() {
        tv_title.setText("切换机器人");
    }

    @Override
    protected void initData(Bundle savedInstanceState) {
        mineModel = new MineModel(this);
        mineModel.setGetRobotListListener(this);
        mineModel.setChangeRobotListener(this);
        lrvRobotList.refresh();
        adapter.setCheckClick(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                RobotDeviceListBean.DataBean item = (RobotDeviceListBean.DataBean) v.getTag();
                if (item == null || "1".equals(item.getCurrent())) {
                    return;
                }
                showProgress();
                JSONObject params = new JSONObject();
                params.put("username", SharePreferenceUtil.getUserName(RobotListActivity.this));
                params.put("device_id", item.getDevice_id());
                mineModel.changeRobot(params, item.getDevice_id());
            }
        });
    }

    private void getData() {
        showProgress();
        mineModel.getRobotList();
    }

    @Override
    public void onGetRobotListSuccess(RobotDeviceListBean baseBean) {
        hideProgress();
        lrvRobotList.refreshComplete(pageSize);
        if (baseBean != null) {
            List<RobotDeviceListBean.DataBean> deviceList = baseBean.getData();
            if (deviceList != null) {
                if (page == 1) {
                    dataList.clear();
                }
                dataList.addAll(deviceList);
//                Collections.sort(dataList, (o1, o2) -> (o1.getCurrent() + "").compareTo(o2.getCurrent()));
                adapter.notifyDataSetChanged();
                recyclerViewAdapter.notifyDataSetChanged();
                return;
            }
        }
        if (baseBean != null && !TextUtils.isEmpty(baseBean.getMsg())) {
            AppUtil.showToast(RobotListActivity.this, baseBean.getMsg());
        } else {
            AppUtil.showToast(RobotListActivity.this, "网络错误，请稍后再试！");
        }
    }

    @Override
    public void onGetRobotListFailure() {
        hideProgress();
        AppUtil.showToast(RobotListActivity.this, "网络错误，请稍后再试！");
    }

    @Override
    protected void onDestroy() {
        if (mineModel != null) {
            mineModel.removeListener();
        }
        super.onDestroy();
    }

    @Override
    public void onChangeRobotSuccess(BaseBean baseBean, String device_id) {
        hideProgress();
        if (baseBean != null) {
            if (baseBean.getCode() == 200) {
                for (RobotDeviceListBean.DataBean dataBean : dataList) {
                    if ((device_id + "").equals(dataBean.getDevice_id())) {
                        dataBean.setCurrent("1");
                        SharePreferenceUtil.setDeviceId(this, device_id);
                        SharePreferenceUtil.setDeviceName(this, dataBean.getName());
                        setResult(Activity.RESULT_OK, new Intent().putExtra("device_id", device_id).putExtra("device_name", dataBean.getName()));
                    } else {
                        dataBean.setCurrent("");
                    }
                }
                adapter.notifyDataSetChanged();
                recyclerViewAdapter.notifyDataSetChanged();
                return;
            }
        }
        if (baseBean != null && !TextUtils.isEmpty(baseBean.getMsg())) {
            AppUtil.showToast(this, baseBean.getMsg());
        } else {
            AppUtil.showToast(this, "切换失败，请重试");
        }
    }

    @Override
    public void onChangeRobotFailure() {
        hideProgress();
        AppUtil.showToast(RobotListActivity.this, "网络错误，请稍后再试！");
    }
}
